package zucc.lcj.android_finalwork;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import pl.com.salsoft.sqlitestudioremote.internal.SQLiteStudioDbService;

public class RegisterActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    EditText user_id,user_pwd1,user_pwd2,user_age;
    Button register;
    Spinner user_sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);


        user_id = (EditText) findViewById(R.id.user_id);
        user_pwd1 = (EditText) findViewById(R.id.user_pwd1);
        user_pwd2 = (EditText) findViewById(R.id.user_pwd2);
        user_age = (EditText) findViewById(R.id.user_age);
        register = (Button) findViewById(R.id.registerBtn);
        user_sex = (Spinner) findViewById(R.id.user_sex);

        register.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                if(!user_pwd1.getText().toString().equals(user_pwd2.getText().toString())){
                    Toast.makeText(getApplicationContext(), "两次密码输入不一致，请重试", Toast.LENGTH_SHORT).show();
                }
//                if(user_pwd1.getText().toString().length()<8 || user_pwd1.getText().toString().length()>16){
//                    Toast.makeText(getApplicationContext(), "密码格式错误，请重试", Toast.LENGTH_SHORT).show();
//                }
                else{
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    Cursor cursor = db.rawQuery("select * from UserInfo",null);
                    if(cursor.moveToFirst()){
                        do{
                            if( cursor.getString(cursor.getColumnIndex("user_name")).equals(user_id.getText().toString())){
                                Toast.makeText(getApplicationContext(), "该用户名已存在", Toast.LENGTH_SHORT).show();
                                break;
                            }
                        }while(cursor.moveToNext());
                    }else{
                        SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                        ContentValues values = new ContentValues();
                        values.put("user_name", user_id.getText().toString());
                        values.put("user_pwd", user_pwd1.getText().toString());
                        values.put("user_header","https://bkimg.cdn.bcebos.com/pic/63d0f703918fa0ec08faacc53fde4eee3d6d54fb6deb?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxODA=,g_7,xp_5,yp_5/format,f_auto");
                        values.put("user_sex", user_sex.getSelectedItem().toString());
                        values.put("user_age", Integer.valueOf(user_age.getText().toString()));
                        values.put("isWechat",0);
                        values.put("helpPost",0);
                        values.put("integral",100);
                        values.put("helpNum",50);
                        values.put("medal",0);
                        db1.insert("UserInfo", null, values);
                        values.clear();
                        AlertDialog alertDialog2 = new AlertDialog.Builder(RegisterActivity.this)
                                .setTitle("恭喜你！")
                                .setMessage("用户创建成功")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent();
                                        intent.setClass(RegisterActivity.this,LoginActivity.class);
                                        startActivity(intent);
                                    }//添加"Yes"按钮
                                })
                                .create();
                        alertDialog2.show();
                    }
                }

//                    Toast.makeText(getApplicationContext(), "创建成功!", Toast.LENGTH_SHORT).show();

            }
        });
    }
}
